﻿using System;
using Algoritmia.Utils;
using Lists = Algoritmia.DataStructures.Lists;

namespace Algoritmia.Problems.Sorting
{
    public class InPlaceSelectionSorter<T> : IInPlaceSorter<T>
    {
        #region IInPlaceSorter<T> Members

        public void Sort(Lists.IList<T> list, Comparison<T> comparison = null)
        {
            comparison = AlgoritmiaFactory.GetDefaultComparison(comparison);

            for(var i = 0; i < list.Count -1 ; i++)
            {
                int minimumIndex = i;
                for(var j = i + 1 ; j < list.Count; j++)
                {
                    if (comparison(list[j], list[minimumIndex]) < 0)
                    {
                        minimumIndex = j;
                    }
                }

                T tmp = list[minimumIndex];
                list[minimumIndex] = list[i];
                list[i] = tmp;
            }
        }

        #endregion
    }
}
